package com.sls.web.manager.game.service.impl;

import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.ProvinceInfo;
import com.sls.web.manager.game.service.ProvinceService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

/**
 * 
 * @Description：
 * @author wxg
 * @Date：2013-6-15
 * 
 */
@Repository("provinceService")
public class ProvinceServiceImpl extends SuperService implements
		ProvinceService {

	private Gson gson;

	public ProvinceServiceImpl() {
		gson = CommonUtil.gson();
	}

	/**
	 * 记录新增
	 * 
	 * @param provinceInfo
	 * @param userInfoVO
	 */
	@Override
	public void addProvinceInfo(ProvinceInfo provinceInfo, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021031"); 
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		
		provinceInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, provinceInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 记录更新
	 * 
	 * @param provinceInfo
	 * @param userInfoVO
	 */
	@Override
	public void editProvinceInfo(ProvinceInfo provinceInfo,
			UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021032"); 
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		provinceInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, provinceInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 分页查询
	 * 
	 * @param provinceInfo
	 * @param userInfoVO
	 * @return
	 */
	@Override
	public Pagination<ProvinceInfo> queryProPagination(
			ProvinceInfo provinceInfo, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021034"); 
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, provinceInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<ProvinceInfo> page = gson.fromJson(body,
					new TypeToken<Pagination<ProvinceInfo>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 根据主键id，查询记录信息
	 * @param id
	 * @param userInfoVO
	 * @return
	 */
	@Override
	public ProvinceInfo queryById(String id, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021033");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		
		ProvinceInfo province = new ProvinceInfo();
		province.setId(id);
		SocketDTO dto = super.send(head, province);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				ProvinceInfo pro = gson.fromJson(body, ProvinceInfo.class);
				return pro;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

}
